            
class ReportParameterController {
    def dataTypeService = new DataTypeService()
	def operatorService = new OperatorService()
	//List dataTypeList = dataTypeService.loadDataType()
	def vNone="None"
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) 
			params.max = 20
			
        [ reportParameterList: ReportParameter.list( params ) ]
    }
	
	def paramHelp = {
        
    }

    def show = {
        def reportParameter = ReportParameter.get( params.id )

        if(!reportParameter) {
            flash.message = "ReportParameter not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ reportParameter : reportParameter ] }
    }

    def delete = {
        def reportParameter = ReportParameter.get( params.id )
		
        if(reportParameter) {
            reportParameter.delete()
            flash.message = "ReportParameter ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "ReportParameter not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def reportParameter = ReportParameter.get( params.id )
		def dataTypeList = dataTypeService.loadDataType()
		
		def operatorList = operatorService.getOperatorList(reportParameter.dataType.toString())
        if(!reportParameter) {
            flash.message = "ReportParameter not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ reportParameter : reportParameter, dataTypeList: dataTypeList, operatorList: operatorList]
        }
    }

    def update = {
        def reportParameter = ReportParameter.get( params.id )
		
        if(reportParameter) {
            reportParameter.properties = params
			reportParameter.setDataType(new Integer(params.dataTypeList.id))			
		    reportParameter.setDefaultOperator(new Integer(params.operatorList.id))
			//5=Image- Not filterable, 6-RTF - Not filterable
			if(params.dataTypeList.id == dataTypeService.imageDataType | 
				params.dataTypeList.id == dataTypeService.rtfDataType)
				reportParameter.setParameterFilterable("N")
			else
			    reportParameter.setParameterFilterable("Y")
				
            if(!reportParameter.hasErrors() && reportParameter.save()) {
                flash.message = "ReportParameter ${params.id} updated"
                redirect(action:show,id:reportParameter.id)
            }
            else {
				render(view:'edit',model:[reportParameter:reportParameter,
					dataTypeList:dataTypeService.loadDataType(),
					operatorList:operatorService.getOperatorList(reportParameter.dataType.toString())])
            }
        }
        else {
            flash.message = "ReportParameter not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }
	
	def refreshOperatorList = {	
		List operatorList = operatorService.getOperatorList(params.dataTypeId)
		//println "new oper list "+operatorList
		render g.select([from:operatorList,name:"operatorList.id",value:"${operatorList?.id.encodeAsHTML()}",
		                 optionKey:"id",optionValue:"codeDesc"])
	}
	
    def create = {
        def reportParameter = new ReportParameter()
        reportParameter.properties = params
		def dataTypeList = dataTypeService.loadDataType()
		Collections.sort(dataTypeList,new ListCompare())
		def operatorList = operatorService.loadBooleanOperator()
		return ['reportParameter':reportParameter,dataTypeList:dataTypeList,operatorList:operatorList]
    }
	
	def save = {
        def reportParameter = new ReportParameter(params)
		def dataTypeList = params.dataTypeList
		//println "dataType.ID = " + dataTypeList.id
		reportParameter.setDataType(new Integer(dataTypeList.id))
		//println "reportParameter.dataType = " + reportParameter.dataType
		def operatorList = params.operatorList
		//println "operatorList.id = " + operatorList.id
		reportParameter.setDefaultOperator(new Integer(operatorList.id))
		//auto set parameterFilterable base on Data Type
		//5=Image- Not filterable, 6-RTF - Not filterable
		if(dataTypeList.id == dataTypeService.imageDataType | 
		   dataTypeList.id == dataTypeService.rtfDataType){
			reportParameter.setParameterFilterable("N")
			reportParameter.setParameterDisplayName(operatorService.getOperatorDesc(OperatorService.none))
		}
		else
			reportParameter.setParameterFilterable("Y")
			
		reportParameter.setDateCreated(new Date())
		
        if(!reportParameter.hasErrors() && reportParameter.save()) {
            flash.message = "ReportParameter ${reportParameter.id} created"
            redirect(action:show,id:reportParameter.id)
        }
        else {
		    if(((dataTypeList.id != dataTypeService.rtfDataType) && (dataTypeList.id != dataTypeService.imageDataType)) && 
				(reportParameter.parameterDisplayName == 
					operatorService.getOperatorDesc(OperatorService.none)))
				reportParameter.setParameterDisplayName(null)
            render(view:'create',model:[reportParameter:reportParameter,
			    dataTypeList:dataTypeService.loadDataType(),
				operatorList:operatorService.getOperatorList(reportParameter.dataType.toString())])
        }
    }
}